package com.googlecode.controlecheque.lista;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.googlecode.controlecheque.R;
import com.googlecode.controlecheque.database.Cheque;
import com.googlecode.controlecheque.enumerador.SituacaoChequeEnum;
import com.googlecode.controlecheque.util.ChequeData;
import com.googlecode.controlecheque.util.DataUtil;

public class ListaChequesExpandivelAdapter extends BaseExpandableListAdapter {

	private DecimalFormat df = new DecimalFormat("###,##0.00");

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	private Context context;

	List<ChequeData> chequesData;

	public ListaChequesExpandivelAdapter(Context context, List<ChequeData> chequesData) {
		this.context = context;
		this.chequesData = chequesData;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return chequesData.get(groupPosition).getCheques().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		Cheque cheque = (Cheque) getChild(groupPosition, childPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.cheque_filho, null);
		}
		TextView txBanco = (TextView) convertView.findViewById(R.id.textoItemBanco);
		txBanco.setText(cheque.nmBanco + " | " + cheque.cdBanco);
		TextView txAgenciaConta = (TextView) convertView.findViewById(R.id.textoItemAgenciaConta);
		txAgenciaConta.setText(cheque.nrAgencia + " | " + cheque.nrConta);
		TextView txNrCheque = (TextView) convertView.findViewById(R.id.textoItemNumeroCheque);
		txNrCheque.setText(cheque.nrCheque);
		TextView txValor = (TextView) convertView.findViewById(R.id.textoItemValorCheque);
		txValor.setText("R$ " + df.format(cheque.valor));
		TextView txSituacao = (TextView) convertView.findViewById(R.id.textoItemSituacaoCheque);
		txSituacao.setText(SituacaoChequeEnum.obterPorValor(cheque.situacao).getDescricao());

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return chequesData.get(groupPosition).getCheques().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return chequesData.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return chequesData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.cheque_grupo, null);
		}

		ChequeData chequeData = (ChequeData) getGroup(groupPosition);
		Date dataVencimento = chequeData.getData();

		Double total = 0.0;
		for (Cheque cheque : chequeData.getCheques()) {
			total += cheque.valor;
		}

		TextView textoGrupo = (TextView) convertView.findViewById(R.id.textoGrupo);
		textoGrupo.setText(DataUtil.formatar(dataVencimento, null));
		TextView grupoQtdCheques = (TextView) convertView.findViewById(R.id.textoGrupoQtdCheques);
		grupoQtdCheques.setText("(" + chequeData.getCheques().size() + ")");
		TextView grupoTotal = (TextView) convertView.findViewById(R.id.textoGrupoTotal);
		grupoTotal.setText("R$ " + df.format(total));

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

}
